package com.fanghaoran.view.manager;

import com.fanghaoran.controller.ManagerController;
import com.fanghaoran.view.user.BackGroundPanel;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.File;
import java.io.IOException;


/**
 * @Author: win11
 * @Date: 2023/03/29/2:01
 * @Description:
 */
public class ModifyWordFrame extends JFrame {
    public ModifyWordFrame(Object[] rowData) throws Exception {
        //最外层界面frame的设置
        initFrame();
        //界面中的视图,控件等的设置
        initView(rowData);

        //设置可见
        this.setVisible(true);
    }
    private void initView(Object[] rowData) throws Exception {
        //panel来放组件
        BackGroundPanel panel = new BackGroundPanel(ImageIO.read(new File("src//image//rBackground.jpg")));
        //用垂直box实现组件垂直排列
        Box vBox = Box.createVerticalBox();
        vBox.add(Box.createVerticalStrut(100));

        Box box1 = Box.createHorizontalBox();
        //单词文本
        JLabel wordText = new JLabel("单词:");
        //单词输入框
        JTextField word = new JTextField(rowData[1].toString(),20);
        //添加到box
        box1.add(wordText);
        box1.add(Box.createHorizontalStrut(20));
        box1.add(word);


        Box box2 = Box.createHorizontalBox();
        //释义文本
        JLabel translationText = new JLabel("释义:");
        //释义输入框
        JTextField translation = new JTextField(rowData[2].toString(),20);
        //添加到box
        box2.add(translationText);
        box2.add(Box.createHorizontalStrut(20));
        box2.add(translation);




        Box box4 = Box.createHorizontalBox();
        //添加按钮
        JButton modifyBtn = new JButton("修改");
        box4.add(modifyBtn);
        //按钮添加点击事件
        modifyBtn.addActionListener(e -> {
                    String result = null;
                    try {
                        result = new ManagerController().modifyWord(word.getText(), translation.getText(),rowData[1].toString()).getResult();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    //对返回结果进行图形化提示
                    if ("输入为空".equals(result)){
                        JOptionPane.showMessageDialog(panel, "输入不能为空");
                    }else {
                        JOptionPane.showMessageDialog(panel, "修改成功");
                        //关闭当前页面
                        this.dispose();
                    }
                }
        );

        vBox.add(box1);
        vBox.add(Box.createVerticalStrut(30));
        vBox.add(box2);
        vBox.add(Box.createVerticalStrut(30));
        vBox.add(box4);

        panel.add(vBox);

        this.add(panel);
    }

    private void initFrame() throws IOException {
        //设置宽高
        this.setSize(400, 400);
        //设置居中
        this.setLocationRelativeTo(null);
        //设置标题
        this.setTitle("扇贝单词");
        //设置标题旁边的logo图标
        this.setIconImage(ImageIO.read(new File("src//image//logo.jpg")));
        //设置置顶
        this.setAlwaysOnTop(true);
        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
}
